package com.fy.tt.dao.impl;

import com.fy.toolhelper.db.BaseDaoImpl;
import com.fy.tt.dao.ParserDao;
import com.fy.tt.entity.Parser;
import com.fy.tt.entity.query.ParserQuery;
import com.fy.tt.entity.vo.ParserVo;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class ParserDaoImpl extends BaseDaoImpl<Parser> implements ParserDao {

    @Override
    public List<Parser> getAllParsers(Connection connection) throws SQLException {
        String sql = "select * from bls_parser";
        PreparedStatement pstm = connection.prepareStatement(sql);
        ResultSet rs = pstm.executeQuery();
        ArrayList<Parser> parsers = new ArrayList<>();
        while(rs.next()){
            Parser parser = new Parser(rs);
            parsers.add(parser);
        }
        if(rs != null){
            rs.close();
        }
        if (pstm != null){
            pstm.close();
        }
        return parsers;
    }


    @Override
    public int deleteParser(Connection connection, int id) throws SQLException {
        String sql = "delete from bls_parser where id = ?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setInt(1, id);
        int i = pstm.executeUpdate();
        if (pstm != null){
            pstm.close();
        }
        return i;
    }


    @Override
    public List<Parser> getPageParsers(Connection connection, int pageSize, int current) throws SQLException{
        int start = pageSize * (current - 1);
        String sql = "select * from bls_parser limit ?,?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setInt(1, start);
        pstm.setInt(2, pageSize);
        ResultSet rs = pstm.executeQuery();
        ArrayList<Parser> parsers = new ArrayList<>();
        while(rs.next()){
            Parser parser = new Parser(rs);
            parsers.add(parser);
        }
        if(rs != null){
            rs.close();
        }
        if (pstm != null){
            pstm.close();
        }
        return parsers;
    }

    @Override
    public List<ParserVo> getConditionPageParsers(Connection connection, ParserQuery parserQuery, int pageSize, int current) throws SQLException{
        int start = pageSize * (current - 1);
        String sql = "select * from bls_parser where 1 = 1 ";

        if(!StringUtils.isEmpty(parserQuery.getName())){
            sql+=" and w.band_name like '%" + parserQuery.getName() + "%'";
            sql+=" or w.description like '%" + parserQuery.getName() + "%' )";
        }


        if(StringUtils.isEmpty(parserQuery.getName())){
            sql = "select * from bls_parser limit ? , ? ";
        }else{
            sql += " limit ? , ?";
        }

        System.out.println("sql = " + sql);

        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setInt(1, start);
        pstm.setInt(2, pageSize);
        ResultSet rs = pstm.executeQuery();
        ArrayList<ParserVo> parsers = new ArrayList<>();
        while(rs.next()){
            ParserVo parser = new ParserVo(rs);
            parsers.add(parser);
        }
        System.out.println(parsers);
        if(rs != null){
            rs.close();
        }
        if (pstm != null){
            pstm.close();
        }
        return parsers;
    }

    @Override
    public Parser getParserByObjId(Connection connection, Long bandObjId) throws SQLException {
        Parser parser = null;
        String sql = "select * from bls_parser where band_obj_id = ? ";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setLong(1,bandObjId);
        ResultSet rs = pstm.executeQuery();
        while (rs.next()){
            parser = new Parser(rs);
        }
        if(rs != null){
            rs.close();
        }
        if (pstm != null){
            pstm.close();
        }
        return parser;
    }


    public ParserDaoImpl() throws Exception {
        super();
    }
}
